package cn.com.nxt.txb;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.netease.nim.uikit.permission.BaseMPermission;
import com.netease.nimlib.sdk.ResponseCode;
import com.netease.nimlib.sdk.avchat.AVChatCallback;
import com.netease.nimlib.sdk.avchat.AVChatManager;
import com.netease.nimlib.sdk.avchat.constant.AVChatType;
import com.netease.nimlib.sdk.avchat.constant.AVChatVideoScalingType;
import com.netease.nimlib.sdk.avchat.model.AVChatData;
import com.netease.nimlib.sdk.avchat.model.AVChatNotifyOption;
import com.netease.nimlib.sdk.avchat.model.AVChatVideoRender;

import java.util.List;

import cn.com.nxt.txb.avchat.AVChatAudio;
import cn.com.nxt.txb.avchat.AVChatSoundPlayer;
import cn.com.nxt.txb.avchat.AVChatUI;
import cn.com.nxt.txb.avchat.AVChatVideo;
import cn.com.nxt.txb.avchat.activity.AVChatExitCode;
import cn.com.nxt.txb.util.AuthPreferences;

public class VideoCallActivity extends BaseActivity {
    private AVChatVideoRender masterRender; // 主播画布
    private FrameLayout mMasterFl;
    private final static String CALL_ACCOUNT="account";
    private String account;

    public static void start(Context context, String account) {
        Intent intent = new Intent(context, VideoCallActivity.class);
        intent.putExtra(CALL_ACCOUNT, account);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_call);
        account=getIntent().getStringExtra(CALL_ACCOUNT);
        masterRender = new AVChatVideoRender(VideoCallActivity.this);
        mMasterFl= (FrameLayout) findViewById(R.id.master_fl);
        mMasterFl.addView(masterRender);
        masterRender.setZOrderMediaOverlay(true);
        AVChatManager.getInstance().enableRtc();
        // 打开视频模块
//        AVChatManager.getInstance().enableVideo();// 设置本地用户视频画布
//        AVChatManager.getInstance().setupLocalVideoRender(masterRender,false, AVChatVideoScalingType.SCALE_ASPECT_BALANCED);// 进行视频预览
//        AVChatManager.getInstance().startVideoPreview();
        AVChatNotifyOption notifyOption = new AVChatNotifyOption();
        notifyOption.extendMessage = "extra_data";
        AVChatManager.getInstance().call2(account, AVChatType.AUDIO, notifyOption, new AVChatCallback<AVChatData>() {
            @Override
            public void onSuccess(AVChatData data) {
                Toast.makeText(VideoCallActivity.this,"success",Toast.LENGTH_SHORT).show();
//                avChatData = data;
//                //DialogMaker.dismissProgressDialog();
//                //如果需要使用视频预览功能，在此进行设置，调用setupLocalVideoRender
//                //如果不需要视频预览功能，那么删掉下面if语句代码即可
//                if (callTypeEnum == AVChatType.VIDEO) {
//                    List<String> deniedPermissions = BaseMPermission.getDeniedPermissions((Activity) context, BASIC_PERMISSIONS);
//                    if (deniedPermissions != null && !deniedPermissions.isEmpty()) {
//                        avChatVideo.showNoneCameraPermissionView(true);
//                        return;
//                    }
//                    initLargeSurfaceView(DemoCache.getAccount());
//                    canSwitchCamera = true;
//                    onCallStateChange(CallStateEnum.OUTGOING_VIDEO_CALLING);
//                }
            }

            @Override
            public void onFailed(int code) {
                Toast.makeText(VideoCallActivity.this,"onFailed",Toast.LENGTH_SHORT).show();

//                LogUtil.d(TAG, "avChat call failed code->" + code);
//                DialogMaker.dismissProgressDialog();

//                AVChatSoundPlayer.instance().stop();
//
//                if (code == ResponseCode.RES_FORBIDDEN) {
//                    Toast.makeText(VideoCallActivity.this, R.string.avchat_no_permission, Toast.LENGTH_SHORT).show();
//                } else {
//                    Toast.makeText(VideoCallActivity.this, R.string.avchat_call_failed, Toast.LENGTH_SHORT).show();
//                }
//                closeSessions(-1);
            }

            @Override
            public void onException(Throwable exception) {
                Toast.makeText(VideoCallActivity.this,"onException",Toast.LENGTH_SHORT).show();

                //LogUtil.d(TAG, "avChat call onException->" + exception);
               // DialogMaker.dismissProgressDialog();

               // AVChatSoundPlayer.instance().stop();
            }
        });
    }

    /**
     * 关闭本地音视频各项功能
     *
     * @param exitCode 音视频类型
     */
//    public void closeSessions(int exitCode) {
//        //not  user  hang up active  and warning tone is playing,so wait its end
//        Log.i("", "close session -> " + AVChatExitCode.getExitString(exitCode));
//        if (avChatAudio != null)
//            avChatAudio.closeSession(exitCode);
//        if (avChatVideo != null)
//            avChatVideo.closeSession(exitCode);
//        showQuitToast(exitCode);
//        isCallEstablish.set(false);
//        canSwitchCamera = false;
//        isClosedCamera = false;
//        aVChatListener.uiExit();
//    }

    private AVChatUI.AVChatListener mAVChatListener =new AVChatUI.AVChatListener(){

        @Override
        public void uiExit() {

        }
    };
}
